<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/commons/global.jsp" %>
<script>
    var organizationTypeGrid;

    obj = {
        editRow : undefined,
        search : function () {
            $('#box').datagrid('load', {

            });
        },
        add : function () {
            $('#save,#redo').show();
            /*
             //当前页行结尾添加
             $('#box').datagrid('appendRow', {
             user : 'bnbbs',
             email : 'bnbbs@163.com',
             date : '2014-11-11',
             });
             */

            if (this.editRow == undefined) {
                //添加一行
                $('#box').datagrid('insertRow', {
                    index : 0,
                    row : {
                        /*
                         user : 'bnbbs',
                         email : 'bnbbs@163.com',
                         date : '2014-11-11',
                         */
                    },
                });

                //将第一行设置为可编辑状态
                $('#box').datagrid('beginEdit', 0);

                this.editRow = 0;
            }
        },
        save : function () {
            //这两句不应该放这里，应该是保存成功后，再执行
            //$('#save,#redo').hide();
            //this.editRow = false;
            //将第一行设置为结束编辑状态
            $('#box').datagrid('endEdit', this.editRow);
        },
        redo : function () {
            $('#save,#redo').hide();
            this.editRow = undefined;
            $('#box').datagrid('rejectChanges');
        },
        edit : function () {
            var rows = $('#box').datagrid('getSelections');
            if (rows.length == 1) {
                if (this.editRow != undefined) {
                    $('#box').datagrid('endEdit', this.editRow);
                }

                if (this.editRow == undefined) {
                    var index = $('#box').datagrid('getRowIndex', rows[0]);
                    $('#save,#redo').show();
                    $('#box').datagrid('beginEdit', index);
                    this.editRow = index;
                    $('#box').datagrid('unselectRow', index);
                }
            } else {
                $.messager.alert('警告', '修改必须或只能选择一行！', 'warning');
            }
        },
        ruledit : function () {
            var rows = organizationTypeGrid.datagrid('getSelected');
            parent.$.modalDialog({
                title : '编辑类型规则',
                width : 500,
                height : 300,
                href : '${path }/org/editTypeRulePage?id=' + rows.id,


            });

        },
        remove : function () {
            var rows = $('#box').datagrid('getSelections');
            if (rows.length > 0) {
                $.messager.confirm('确定操作', '您正在要删除所选的记录吗？', function (flag) {
                    if (flag) {
                        var ids = [];
                        for (var i = 0; i < rows.length; i ++) {
                            ids.push(rows[i].id);
                        }
                        //console.log(ids.join(','));
                        $.ajax({
                            type : 'POST',
                            url : '/org/deleteorgtype',
                            data : {
                                ids : ids.join(','),
                            },
                            beforeSend : function () {
                                $('#box').datagrid('loading');
                            },
                            success : function (data) {
                                if (data) {
                                    $('#box').datagrid('loaded');
                                    $('#box').datagrid('load');
                                    $('#box').datagrid('unselectAll');
                                    $.messager.show({
                                        title : '提示',
                                        msg : data + '个用户被删除成功！',
                                    });
                                }
                            },
                        });
                    }
                });
            } else {
                $.messager.alert('提示', '请选择要删除的记录！', 'info');
            }
        },
    };
    $(function () {

        organizationTypeGrid= $('#box').datagrid({
            url : '${path }/org/orgTypelist',
            idField : 'id',
            //url : 'content.json',
            title : '组织机构类型列表',
            iconCls : 'icon-search',
            striped : true,
            nowrap : true,
            striped: true,
            rownumbers : true,
            fitColumns : true,
            frozenColumns : [ [ {
                title : 'id',
                field : 'id',
                width : 40,
                hidden : true
            } ] ],
            columns : [[
                {
                    field : 'name',
                    title : '机构类型名称',
                    sortable : true,
                    width : 100,
                    editor : {
                        type : 'validatebox',
                        options : {
                            required : true,
                        },
                    },
                },
                {
                    field : 'sn',
                    title : '机构类型简码',
                    sortable : true,
                    width : 100,
                    editor : {
                        type : 'validatebox',
                        options : {
                            required : true,
                        },
                    },
                },

            ]],
            toolbar : '#tb',
            pagination : true,
            pageSize : 10,
            pageList : [10, 20, 30],
            pageNumber : 1,

            onAfterEdit : function (rowIndex, rowData, changes) {
                $('#save,#redo').hide();

                var inserted = $('#box').datagrid('getChanges', 'inserted');
                var updated = $('#box').datagrid('getChanges', 'updated');

                var url = info =  '';

                //新增用户
                if (inserted.length > 0) {
                    url = '/org/addorgtype';
                    info = '新增';
                }

                //修改用户
                if (updated.length > 0) {
                    url = '/org/updateorgtype';
                    info = '修改';
                }

                $.ajax({
                    type : 'POST',
                    url : url,
                    data :JSON.stringify(rowData),
                    dataType:"json",
                    contentType:"application/json",
                    beforeSend : function () {
                        $('#box').datagrid('loading');
                        console.log(rowData);
                    },
                    success : function (data) {
                        if (data) {
                            $('#box').datagrid('loaded');
                            $('#box').datagrid('load');
                            $('#box').datagrid('unselectAll');
                            $.messager.show({
                                title : '提示',
                                msg : '数据' + info + '成功！',
                                style:{
                                    right:'',
                                    top:document.body.scrollTop+document.documentElement.scrollTop,
                                    bottom:''
                                }

                            });
                            obj.editRow = undefined;
                        }
                    },
                });
                //console.log(rowData);
            },

        });

    });



</script>
<table id="box"></table>

<div id="tb" style="padding:5px;">
    <div style="margin-bottom:5px;">
        <shiro:hasPermission name="+org+8">
            <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="obj.add();">添加</a>
        </shiro:hasPermission>
        <shiro:hasPermission name="+org+4">
            <a href="#" id="edit" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="obj.edit();">修改</a>
        </shiro:hasPermission>
        <shiro:hasPermission name="+org+4">
            <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="obj.remove();">删除</a>
            <a href="#" class="easyui-linkbutton" iconCls="e-icon fa fa-book fa-lg"  plain="true" onclick="obj.ruledit();">设置类型规则</a>
        </shiro:hasPermission>
        <a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true" style="display:none;" id="save" onclick="obj.save();">保存</a>
        <a href="#" class="easyui-linkbutton" iconCls="icon-redo" plain="true" style="display:none;" id="redo" onclick="obj.redo();">取消编辑</a>

    </div>
    <div style="padding:0 0 0 7px;color:#333;">
        组织机构类型：<input type="text" class="textbox" name="user" style="width:110px">
        创建时间从：<input type="text" name="date_from" class="easyui-datebox" style="width:110px">
        到：<input type="text" name="date_to" class="easyui-datebox" style="width:110px">
        <a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="obj.search();">查询</a>
    </div>
</div>
